﻿
using UnityModule;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEditor;

public class UnionBundle
{
    static string assetOutPath = Path.Combine(Directory.GetCurrentDirectory(), BuildBundles.OUT_PATH, EditorPlatform.GetPlatformName());
    static List<byte> BundleBuffer = new List<byte>();

    public static void Merge(Dictionary<string, AssetBundleInfoV2> bundleBinInfo, long ver)
    {
        BundleBuffer.Clear();
        BundleBuffer.AddRange(BitConverter.GetBytes(ver));
        EditorUtility.DisplayProgressBar("合并资源包", "UnionBundle", 0);
        int i = 0;
        foreach (var info in bundleBinInfo)
        {
            if (info.Key == "client_resversion" || info.Key == "client_download" || info.Key == "http_resume_support")
            {
                continue;
            }
            string filePath = Path.Combine(assetOutPath, info.Value.abNameWithHash);
            byte[] FileBytes = File.ReadAllBytes(filePath);
            info.Value.offsetBegin = BundleBuffer.Count;
            BundleBuffer.AddRange(FileBytes);
            info.Value.offsetEnd = BundleBuffer.Count;
            i++;
            EditorUtility.DisplayProgressBar("合并资源包", filePath, (float)(i * 1.0 / bundleBinInfo.Count));
        }
        File.WriteAllBytes(Path.Combine(assetOutPath, $"UnionBundle_{ver}.update"), BundleBuffer.ToArray());
        EditorUtility.ClearProgressBar();
    }
}

